function e(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function t(e) {
    Object.assign(E, e);
}

function n(e) {
    y.default.addListener(T, e);
}

function o(e) {
    wx.showLoading({
        title: "登录中...",
        mask: !0
    });
    var t = function() {
        wx.hideLoading();
    }, n = c().then(function(t) {
        return I.register(p({}, E, t, e));
    });
    return n.then(function(e) {
        return j = w.default.resolve(e.data);
    }), n.then(t, t), n.then(l).catch(d);
}

function r(e, t) {
    if (!j) {
        wx.showLoading({
            title: "登录中..."
        });
        var n = O.get(), o = function(e) {
            wx.hideLoading(), g();
        }, r = n ? a({
            loginkey: n
        }) : c().then(function(e) {
            return a(e);
        });
        r.then(o, o), j = r.then(l).catch(d);
    }
    return j.then(e, t), j;
}

function i() {
    return m;
}

function s(e) {
    var t = e.accesstoken, n = e.avatar, o = e.loginkey, r = e.openid, i = e.realname, s = e.uid, a = e.unionid, u = e.userid, c = e.corpid, f = e.isverifymobile, l = e.sessionkey;
    Object.assign(m, {
        accesstoken: t,
        avatar: n,
        loginkey: o,
        openid: r,
        realname: i,
        uid: s,
        unionid: a,
        userid: u,
        corpid: c,
        isverifymobile: f,
        sessionkey: l
    }), o && O.set(o), t && y.default.emit(T, m);
}

function a(e) {
    var t = p({}, E, {
        code: ""
    }, e);
    return I.login(t);
}

function u() {
    return new w.default(function(e, t) {
        (wx.qy && wx.qy.login || wx.login)({
            success: function(t) {
                var n = t.code;
                return e({
                    code: n
                });
            },
            fail: t
        });
    });
}

function c() {
    return new w.default(function(e, t) {
        (wx.qy && wx.qy.checkSession || wx.checkSession)({
            success: function() {
                m.sessionkey ? e({
                    sessionkey: m.sessionkey
                }) : u().then(e, t);
            },
            fail: function() {
                m.sessionkey = "", u().then(e, t);
            }
        });
    });
}

function f() {
    return O.remove(), j = null, new w.default(function(e, t) {
        ++q < S ? setTimeout(function() {
            e(r());
        }, _) : t();
    });
}

function l(e) {
    var t = k.default.clearBOMAndParseJson(e), n = t.code, o = t.data, r = t.message;
    switch (console.log(e, "登录结果"), n) {
      case x.default.SUCCESS:
      case x.default.NO_BINDING_USER:
      case x.default.MOBILE_IS_NOT_VERIFY:
        if (!o) throw new Error("api:login:fail no data");
        return s(Object.assign({}, m, o)), w.default.resolve(m);

      case x.default.USER_ACCOUNT_NOT_EXISTS:
        return s(o), w.default.resolve();

      case x.default.USER_ACCOUNT_ALREADY_EXISTS:
      case x.default.TOKEN_EXPIRY:
      case x.default.PARAM_MISSING:
        return f().catch(function(e) {
            throw new Error("api:login:fail " + n + " " + r);
        });

      default:
        throw new Error("api:login:fail " + n + " " + r);
    }
}

function d(e) {
    var t = e.errMsg, n = e.message;
    return j = null, wx.showModal({
        title: "微信登录失败",
        content: "请尝试重新打开小程序，或联系客服解决(错误原因：" + (t || n) + ")"
    }), w.default.reject(e);
}

function g(e) {
    var t = wx.getStorageSync("last_post_device_info_time");
    (!t || Date.now() - t > 864e5) && wx.getNetworkType({
        success: function(e) {
            var t = e.networkType;
            wx.getSystemInfo({
                success: function(e) {
                    wx.setStorage({
                        key: "last_post_device_info_time",
                        data: Date.now()
                    }), I.deviceInfo(p({
                        networkType: t
                    }, e));
                }
            });
        }
    });
}

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.api = void 0;

var p = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
        var n = arguments[t];
        for (var o in n) Object.prototype.hasOwnProperty.call(n, o) && (e[o] = n[o]);
    }
    return e;
};

exports.setDefaultParams = t, exports.listenToken = n, exports.registerToken = o, 
exports.getTokens = r, exports.getTokensSync = i, exports.updateTokens = s, exports.getWxLoginCode = u, 
exports.getLoginCodeOrSessionkey = c;

var w = e(require("./promise.js")), k = e(require("./util.js")), y = e(require("./event-emitter")), x = e(require("./api-code.js")), v = require("./host.js"), h = require("./request.js"), S = 3, _ = 3e3, T = "user.get.token", m = {}, O = {
    key: "loginkey",
    get: function() {
        return wx.getStorageSync(this.key);
    },
    set: function(e) {
        e !== this.get() && wx.setStorage({
            key: this.key,
            data: e
        });
    },
    remove: function() {
        wx.removeStorageSync(this.key);
    }
}, E = {}, I = exports.api = {
    login: function(e) {
        return (0, h.post)(v.apiPrefix + "/weapp/loginv2", e);
    },
    register: function(e) {
        return (0, h.post)(v.apiPrefix + "/weapp/register", e);
    },
    deviceInfo: function(e) {
        return (0, h.post)(v.apiPrefix + "/weapp/deviceinfo", e);
    }
}, j = null, q = 0;

exports.default = {
    api: I,
    listenToken: n,
    registerToken: o,
    setDefaultParams: t,
    getTokens: r,
    getTokensSync: i,
    updateTokens: s,
    getLoginCodeOrSessionkey: c
};